package controller

import (
	backend "AccessAdmin/api/backend"
	"AccessAdmin/internal/model"
	"AccessAdmin/internal/model/entity"
	"AccessAdmin/internal/service"
	"context"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
)

// 角色管理
var Role = cRole{}

type cRole struct{}

// 渲染角色列表
func (c *cRole) ShowIndex(ctx context.Context, req *backend.RoleIndexReq) (res *backend.RoleIndexRes, err error) {
	service.View().Render(ctx, model.View{
		Title: "角色列表",
	})
	return
}

// 获取角色列表
func (c *cRole) IndexData(ctx context.Context, req *backend.RoleListReq) (res *backend.RoleListRes, err error) {
	res = &backend.RoleListRes{}
	_data, err := service.Role().GetList(ctx, model.RoleGetListInput{
		Page:     req.Page,
		Size:     req.Limit,
		Name:     req.Name,
		Identify: req.Identify,
		Status:   req.Status,
	})
	if _data != nil && err == nil {
		res.Data = _data.List
		res.Count = _data.Total
	}
	return
}

// 显示角色创建
func (c *cRole) ShowCreate(ctx context.Context, req *backend.RoleShowCreateReq) (res *backend.RoleShowCreateRes, err error) {
	service.View().Render(ctx, model.View{
		Title:   "角色创建",
		MainTpl: "role/form.html",
		Data: g.Map{
			"action":    "/admin/role/create",
			"roleModel": &entity.Role{},
		},
	})
	return
}

// 创建角色
func (c *cRole) Create(ctx context.Context, req *backend.RoleCreateReq) (res *backend.RoleCreateRes, err error) {
	out, err := service.Role().Create(ctx, model.RoleCreateInput{
		RoleCreateUpdateBase: model.RoleCreateUpdateBase{
			Name:     req.Name,
			Identify: req.Identify,
			Status:   req.Status,
			Sort:     req.Sort,
			Remark:   req.Remark,
		},
	})
	if err != nil {
		return nil, err
	}
	return &backend.RoleCreateRes{RoleId: out.RoleId}, nil
}

// 显示角色修改
func (c *cRole) ShowUpdate(ctx context.Context, req *backend.RoleShowUpdateReq) (res *backend.RoleShowUpdateRes, err error) {
	roleModel, err := service.Role().GetDetail(ctx, model.RoleGetDetailInput{
		Id: req.Id,
	})
	if err != nil {
		return nil, gerror.New("角色信息错误!")
	}
	service.View().Render(ctx, model.View{
		Title:   "角色修改",
		MainTpl: "admin/role/form.html",
		Data: g.Map{
			"action":    "/admin/role/update/" + gconv.String(roleModel.Id),
			"roleModel": roleModel,
		},
	})
	return
}

// 修改角色
func (c *cRole) Update(ctx context.Context, req *backend.RoleUpdateReq) (res *backend.RoleUpdateRes, err error) {
	err = service.Role().Update(ctx, model.RoleUpdateInput{
		Id: req.Id,
		RoleCreateUpdateBase: model.RoleCreateUpdateBase{
			Name:     req.Name,
			Identify: req.Identify,
			Status:   req.Status,
			Sort:     req.Sort,
			Remark:   req.Remark,
		},
	})
	return
}

// 删除角色
func (c *cRole) Delete(ctx context.Context, req *backend.RoleDeleteReq) (res *backend.RoleDeleteRes, err error) {
	err = service.Role().Delete(ctx, model.RoleGetDetailInput{
		Id: req.Id,
	})
	return
}

// 查看角色
func (c *cRole) ShowView(ctx context.Context, req *backend.RoleShowViewReq) (res *backend.RoleShowViewRes, err error) {
	roleModel, err := service.Role().GetDetail(ctx, model.RoleGetDetailInput{
		Id: req.Id,
	})
	if err != nil {
		return nil, gerror.New("角色信息错误!")
	}
	service.View().Render(ctx, model.View{
		Title:   "角色查看",
		MainTpl: "admin/role/view.html",
		Data: g.Map{
			"roleModel": roleModel,
		},
	})
	return
}

// 获取角色通过XmSelect
func (c *cRole) GetRolesByXmSelect(ctx context.Context, req *backend.RoleXmSelectReq) (res *backend.RoleXmSelectRes, err error) {
	res = &backend.RoleXmSelectRes{}
	out, err := service.Role().GetListByXmSelect(ctx, req.UserId)
	if err != nil {
		return res, err
	}
	for _, item := range out {
		res.Items = append(res.Items, backend.CommonXmSelectRes{
			Name:     item.Name,
			Value:    item.Value,
			Selected: item.Selected,
		})
	}
	return
}

// 显示角色授权
func (c *cRole) ShowAuth(ctx context.Context, req *backend.RoleShowAuthReq) (res *backend.RoleShowAuthRes, err error) {
	res = &backend.RoleShowAuthRes{}
	out, err := service.Menu().GetRoleMenus(ctx, req.Id)
	if err != nil {
		return res, err
	}
	res.Items = out
	return
}

// 授权角色
func (c *cRole) Auth(ctx context.Context, req *backend.RoleAuthReq) (res *backend.RoleAuthRes, err error) {
	err = service.Role().Auth(ctx, model.RoleAuthInput{
		Id:     req.Id,
		AuthId: req.AuthId,
	})
	return
}
